#!/usr/bin/env python
# -*- coding=utf-8 -*-
__author__ = 'man'

#		_________________________________________________________________________________
#
#						漫鸽网络技术服务(mangenotwork)拥有所有权
#						@www.hbsj.xyz   
#						@www.mangenotwork.com
#			严重声明：原始代码属于"漫鸽网络技术服务(mangenotwork)"的知识产权，拥有法律的保护，
#		请勿抄袭、转载、删除、私用、篡改、破坏...
#
#       
#		简介 ：  public.py API公用方法
#		作者 ： 李漫(man)
#		创建时间:  20190620
#		
#		_________________________________________________________________________________


from flask import Flask,request,g,render_template,redirect,url_for,abort,session,jsonify,current_app
from flask import Blueprint , render_template
from flask import make_response,Response 
import sys,os
import json
import time
import datetime
import random
import redis
from cryptography.fernet import Fernet
import hashlib
import db.mandb



'''
					#类型： DB交互
					#执行查询  查询结果
'''
def ManDB(func=None, param=None):
	def deco(func):
		def wrapper(*args,**kwargs):
			man_db = db.mandb.DB()
			return func(man_db,*args,**kwargs)			
		wrapper.__name__ = func.__name__
		return wrapper
	return deco if not func else deco(func)


'''
		function : man_token_create(strings)
		@strings 字符串
		描述： 生成 token 加密 密文
		Return : 
'''
def man_token_create(strings):
	key = Fernet.generate_key()
	#print(key)
	man_token_string = Fernet(key)
	set_b = bytes(strings, encoding = "utf8")
	token_string_b = man_token_string.encrypt(set_b)
	keys = str(key, encoding = "utf8")
	return str(token_string_b, encoding = "utf8"),keys


'''		
		function : man_token_decode(strings,key)
		@strings : 密文
		@key : 秘钥
		#解密 token 解密成明文
'''
def man_token_decode(strings,key):
	#print(key)
	key_s = bytes(key, encoding = "utf8")
	man_token_string = Fernet(key_s)
	set_s = bytes(strings, encoding = "utf8")
	token_string_s = man_token_string.decrypt(set_s)
	return token_string_s

'''		
		function : man_token_decode2(strings,key)
		@strings : 密文
		@key : 秘钥 b''
		#解密 token 解密成明文
'''
def man_token_decode2(strings,key_b):
	#print(key)
	key_s = key_b
	man_token_string = Fernet(key_s)
	set_s = bytes(strings, encoding = "utf8")
	token_string_s = man_token_string.decrypt(set_s)
	return str(token_string_s, encoding = "utf8")


'''		
		function : man_token_decode2(strings,key)
		@strings : 密文
		@key : 秘钥 b''
		#解密 token 解密成明文
'''
def man_token_decode3(strings,key_b):
	#print(key)
	key_s = key_b
	man_token_string = Fernet(key_s)
	set_s = bytes(strings, encoding = "utf8")
	token_string_s = man_token_string.decrypt(set_s)
	return token_string_s

'''
		function : set_user_uuid(src)
		@src : 需要转换md5值的字符串
		#设置用户的uuid
'''
def set_user_uuid(src):
	m2 = hashlib.md5()   
	m2.update(src.encode('utf-8'))   
	useruuid = m2.hexdigest()
	return useruuid


'''
		function : userACC()
		#用户身份识别
'''
def userACC():
    userToken=request.cookies.get('user_token')
    print("[Get Cookies] = "+str(userToken))
    username=request.cookies.get('user_name')
    print("[Get username] = "+str(username))

    keys = session.get(userToken)
    print("[Get keys] = "+str(keys))
    if userToken != None and keys == username:
        return True
    else:
        return False

def HEAD_IMG_Main_Path():
	#Main_Path = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) +"/static/img_head/"
	Main_Path = "C:/images/user_head/"
	print(Main_Path)
	return Main_Path


def INFO_IMG_Main_Path():
	#Main_Path = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) +"/static/img_info/"
	Main_Path = "C:/images/info/"
	print(Main_Path)
	return Main_Path


#时间戳转换日期
def time_to_date(times):
	timeStamp = float(times)
	timeArray = time.localtime(timeStamp)
	otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
	print(otherStyleTime)
	return str(otherStyleTime)


#获取访问总量
@ManDB
def get_PV( man_db):
	sql = """
		SELECT Max(count_id) from count_query;
	"""
	return_datas = man_db.execute_seles(sql)
	return return_datas[0][0]


